package org.dochandler.faces.bean;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.dochandler.config.DHConfig;
import org.dochandler.faces.servicefactory.MissionRequestServiceFacesFactory;
import org.dochandler.faces.util.FacesNav;
import org.dochandler.service.MissionRequestService;
import org.dochandler.service.domain.mission.MissionRequest;
import org.esfinge.guardian.exception.AuthorizationException;

@SuppressWarnings("serial")
@ManagedBean
@RequestScoped
public class MissionRequestBean implements Serializable {
	private String requestDescrition;
	private String missionStart;
	private String missionEnd;
	private Double estimatedCost;
	private MissionRequestService missionRequestService;
	
	public MissionRequestBean() {
		setMissionRequestService( MissionRequestServiceFacesFactory.getInstance().create( getFacesCtx() ));
	}
	
	public String requestMission() {
		String pageKey = FacesNav.MISSION_FUNCIONS_LIST.pageKey();
		
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		
		MissionRequest missionRequest = new MissionRequest();
		missionRequest.setRequestDescrition( getRequestDescrition() );
		missionRequest.setEstimatedCost( getEstimatedCost() );
		try {
			missionRequest.setMissionStart( df.parse(getMissionStart() ) );
			missionRequest.setMissionEnd( df.parse(getMissionEnd() ) );
		} catch (ParseException e) {
			String exceptionMsg = DHConfig.getString("msg_invalid_date");
			getFacesCtx().addMessage("main_form", new FacesMessage(exceptionMsg));
			pageKey = FacesNav.REQUEST_MISSION.pageKey();
		}
		
		try {
			getMissionRequestService().requestMission(missionRequest);
		} catch(AuthorizationException e) {
			String exceptionMsg = DHConfig.getString("msg_unauthorized");
			getFacesCtx().addMessage("main_form", new FacesMessage(exceptionMsg));
		}
		
		return pageKey;
	}
	
	private FacesContext getFacesCtx() {
		return FacesContext.getCurrentInstance();
	}
	
	/*
	 * SETTERS AND GETTERS
	 */

	public String getRequestDescrition() {
		return requestDescrition;
	}

	public void setRequestDescrition(String requestDescrition) {
		this.requestDescrition = requestDescrition;
	}

	public String getMissionStart() {
		return missionStart;
	}

	public void setMissionStart(String missionStart) {
		this.missionStart = missionStart;
	}

	public String getMissionEnd() {
		return missionEnd;
	}

	public void setMissionEnd(String missionEnd) {
		this.missionEnd = missionEnd;
	}

	public Double getEstimatedCost() {
		return estimatedCost;
	}

	public void setEstimatedCost(Double estimatedCost) {
		this.estimatedCost = estimatedCost;
	}

	public MissionRequestService getMissionRequestService() {
		return missionRequestService;
	}

	public void setMissionRequestService(MissionRequestService missionRequestService) {
		this.missionRequestService = missionRequestService;
	}
	
}
